﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Emoney.DataAccess;
using LiuYan.Base;
using LiuYan.Entity;


namespace LiuYan.Service
{
    public class AdminService
    {
        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Edit(TB_Admin entity)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Id", SqlDbType.BigInt),
                new SqlParameter("@UserName", SqlDbType.VarChar,50),
                new SqlParameter("@Password", SqlDbType.VarChar,50),
                new SqlParameter("@IsExit", SqlDbType.Int),
                new SqlParameter("@Tell", SqlDbType.VarChar, 50),
                new SqlParameter("@RoleId", SqlDbType.Int),
                new SqlParameter("@Remark", SqlDbType.VarChar, 150),
                new SqlParameter("@Date", SqlDbType.DateTime),
                 new SqlParameter("@Name", SqlDbType.VarChar,50),
            };
            parameters[0].Value = entity.Id;
            parameters[1].Value = entity.UserName;
            parameters[2].Value = new MD5().Md5Get32(entity.PassWord).ToLower();
            parameters[3].Value = entity.IsExit;
            parameters[4].Value = entity.Tell;
            parameters[5].Value = entity.RoleId;
            parameters[6].Value = entity.Remark;
            parameters[7].Value = entity.Date;
            parameters[8].Value = entity.Name;
            string sql = entity.Id == 0 ? "insert into dbo.TB_Admin(Password,IsExit,UserName,Tell,RoleId,Remark,Date,Del,Name)values" +
                "(@Password,@IsExit,@UserName,@Tell,@RoleId,@Remark,@Date,0,@Name)"
                : " update dbo.TB_Admin set "+
                  "IsExit=@IsExit,UserName=@UserName,Tell=@Tell,RoleId=@RoleId,Remark=@Remark,Name=@Name where id=@Id";
            return SqlHelper.ExecuteNonQuery(Connection.conStr, CommandType.Text, sql, parameters);
        }

        /// <summary>
        /// 验证用户名是否重复
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public int CheckRegistUser(string username)
        {
            string sql = "select count(*) from TB_Admin where username='" + username + "'";
            var obj = SqlHelper.ExecuteScalar(Connection.conStr, CommandType.Text, sql);
            return Convert.ToInt32(obj);
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public DataSet GetList(int pageIndex, int pageCount, string userName,string name)
        {
            SqlParameter[] parameter=new SqlParameter[]
            {
                new SqlParameter("@userName",SqlDbType.VarChar,50),
                new SqlParameter("@name",SqlDbType.VarChar,50),
            };
            parameter[0].Value = userName;
            parameter[1].Value = name;
            string where = "";
            where = name == "" ? where : ("and name like  N'%'+@name+'%' ");
            where += userName == "" ? where : ("and userName like  N'%'+@userName+'%' ");
            string sql = " select COUNT(*) as count from TB_Admin"+(where==""?where:(" where "+where.Substring(3,where.Length-3)))+
                         " select *,(select  RoleName from dbo.TB_Roles where id=TB_Admin.RoleId) as RoleName " +
                         " from dbo.TB_Admin where id not in (select top(" + pageIndex + ") Id from dbo.TB_Admin order by id desc) and Del=0  " + where + " order by id desc ";
            var set = SqlHelper.ExecuteDataset(Connection.conStr, CommandType.Text, sql, parameter);
            set.Tables[0].TableName = "count";
            set.Tables[1].TableName = "data";
            return set;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public int RevertPwd(int userid)
        {
            string sql = "update TB_Admin set Password='" + new MD5().Md5Get32("111111").ToLower() + "' where id=" + userid;
            return SqlHelper.ExecuteNonQuery(Connection.conStr,CommandType.Text,sql);
        }
    }
}
